import http from '@/api/http.js'
export default async function(imgstr,urlArr=[]){
	let params, infoRes, path;
	if(Array.isArray(imgstr)){ //多张上传
		return new Promise(resolve => {
			imgstr.map(async (item, index) => {
				// #ifdef MP-WEIXIN
				path = 'data:image/jpeg;base64,' + wx.getFileSystemManager().readFileSync(item, 'base64');
				// #endif
				// #ifdef MP-TOUTIAO
				path = 'data:image/jpeg;base64,' + tt.getFileSystemManager().readFileSync(item, 'base64');
				// #endif
				// #ifdef H5 || MP-ALIPAY
				path = item
				// #endif
				params = {
					method: "api/v2/common/upload_img",
					imgstr: path
				}
				infoRes = await http(params);
				if(infoRes.status){
					urlArr.push(infoRes.data.imgurl)
					if(index == imgstr.length - 1){
						setTimeout(() => {
							uni.hideLoading()
						}, 1000)
					}
				}else{
					uni.showToast({
						title: infoRes.msg,
						icon: "none"
					})
				}
			})
			resolve(urlArr)
		})
	}else{ //单张上传
		return new Promise(async resolve => {
			// #ifdef MP-WEIXIN
			path = 'data:image/jpeg;base64,' + wx.getFileSystemManager().readFileSync(imgstr, 'base64');
			// #endif
			// #ifdef MP-TOUTIAO
			path = 'data:image/jpeg;base64,' + tt.getFileSystemManager().readFileSync(imgstr, 'base64');
			// #endif
			// #ifdef H5 || MP-ALIPAY
			path = imgstr
			// #endif
			params = {
				method: "api/v2/common/upload_img",
				imgstr: path
			}
			infoRes = await http(params);
			if(infoRes.status){
				resolve(infoRes.data.imgurl)
				setTimeout(() => {
					uni.hideLoading()
				}, 500)
			}else{
				uni.showToast({
					title: infoRes.msg,
					icon: "none"
				})
			}
		})
	}
}